package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {

    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    public static final String PERMISSION_IN_SESSION="PERMISSION_IN_SESSION";

    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = requestAttributes.getRequest().getSession();
        return session;
    }

    /**
     * 获取到当前登录的用户
     * @return
     */
    public static Employee getCurrentUser(){

        return (Employee)getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }

    /**
     * 设置用户到session中
     * @return
     * @param employee
     */
    public static void setCurrentUser(Employee employee){
        getSession().setAttribute(EMPLOYEE_IN_SESSION,employee);
    }

    /**
     *
     * @return
     */
    public static List<String> getPermissionExpression(){
        return (List<String>)getSession().getAttribute(PERMISSION_IN_SESSION);
    }
    /**
     * 设置当前用户的权限到session中
     * @param strings
     */
    public static void setPermissionExpression(List<String> strings){
        getSession().setAttribute(PERMISSION_IN_SESSION,strings);
    }
}
